/* 
 * CDDL HEADER START
 * The contents of this file are subject to the terms
 * of the Common Development and Distribution License 
 * (the License). You may not use this file except in
 * compliance with the License.
 *
 * You can obtain a copy of the License at
 * http://www.sun.com/cddl/cddl.html and legal/CDDLv1.0.txt
 * See the License for the specific language governing
 * permission and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL 
 * Header Notice in each file and include the License file  
 * at legal/CDDLv1.0.txt.                                                           
 * If applicable, add the following below the CDDL Header,
 * with the fields enclosed by brackets [] replaced by
 * your own identifying information: 
 * "Portions Copyrighted [year] [name of copyright owner]"
 *
 * Copyright 2006 Sun Microsystems Inc. All Rights Reserved
 * CDDL HEADER END
 * 
 * Portions Copyrighted 2009 Lumis EIP
 */
// This class is a copy of com.sun.portal.wsrp.producer.router.RouterServlet with some minor adjustments.

package lumis.service.wsrp.producer;

import javax.servlet.ServletException;
import javax.servlet.RequestDispatcher;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.sun.portal.wsrp.producer.ProducerException;

/**
 * Routes requests to the webservice servlet.
 *
 * @version $Revision: 11180 $ $Date: 2009-12-04 18:13:02 -0200 (Fri, 04 Dec 2009) $
 * @since 5.5.0
 */
@SuppressWarnings("serial")
public class ProducerRouterServlet extends HttpServlet
{
	public static final String WSRP_PRODUCER_KEY = "wsrp.producer.key";

	private static class Identifier
	{
		private String key = null;

		private String service = null;

		public Identifier(HttpServletRequest req) throws ProducerException
		{
			String pathinfo = req.getPathInfo();
			if (pathinfo == null || pathinfo.length() == 0)
			{
				throw new ProducerException("pathinfo was null");
			}

			// remove leading "/"
			pathinfo = pathinfo.substring(1);

			// find index of slash between the key and service
			int i = pathinfo.indexOf("/");

			// find index of slash between the service and version i.e (v1 or v2)
			i = pathinfo.indexOf("/", i + 1);

			if (i == -1)
			{
				throw new ProducerException("invalid format pathinfo=" + pathinfo);
			}

			service = pathinfo.substring(0, i);
			key = pathinfo.substring(i + 1);
		}

		public String getKey()
		{
			return key;
		}

		public String getService()
		{
			return service;
		}
	}

	protected void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, java.io.IOException
	{
		try
		{
			Identifier id = new Identifier(req);

			req.setAttribute(WSRP_PRODUCER_KEY, id.getKey());
			RequestDispatcher rd = req.getRequestDispatcher("/lumis/service/wsrp/producer/service/" + id.getService());
			rd.forward(req, res);
		}
		catch (ProducerException e)
		{
			res.sendError(HttpServletResponse.SC_NOT_FOUND);
		}
	}
}
